package com.ruoyi.shop.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.shop.api.domain.CheShop;
import com.ruoyi.shop.domain.pojo.AddListImage;
import com.ruoyi.shop.domain.pojo.RegisterShop;
import com.ruoyi.shop.service.ICheShopImageService;
import com.ruoyi.shop.service.ICheShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName ByPhoneController
 * @Author Jovan HOU
 * @Date 2025年01月11日 20:09
 */
@RestController
@RequestMapping
public class ByPhoneController
{
    @Autowired
    private  ICheShopService cheShopService;
    @Autowired
    private ICheShopImageService cheShopImageService;



//    @GetMapping("/byphone")
//    public AjaxResult listbyphone(@RequestParam("phone") String  phone)
//    {
//        CheShop cheShop = cheShopService.selectCheShopByPhone(phone);
//        return AjaxResult.success(cheShop);
//    }
@PostMapping("/register")
@Transactional(rollbackFor = Exception.class)
public R<?> register(@RequestBody  RegisterShop registerShop) {


    CheShop cheShop = new CheShop();

    cheShop.setShopNumber(registerShop.getShopNumber());
    cheShop.setShopArea(registerShop.getShopArea());
    cheShop.setShopAddress(registerShop.getShopAddress());
    cheShop.setShopIntro(registerShop.getShopIntro());
    cheShop.setShopLicence(registerShop.getShopLicence());
    cheShop.setCardFront(registerShop.getCardFront());
    cheShop.setCardBack(registerShop.getCardBack());
    cheShop.setPerName(registerShop.getPerName());
    cheShop.setPerPhone(registerShop.getPerPhone());
    cheShop.setPerIdcard(registerShop.getPerIdcard());
    cheShop.setStoreName(registerShop.getStoreName());





    // 手机号唯一性校验
    String perPhone = cheShop.getPerPhone();
    if (cheShopService.selectCheShopByPhone(perPhone) != null) {
        return R.fail("手机号已被注册");
    }

    // 插入商铺信息
    int result = cheShopService.insertCheShop(cheShop);
    if (result == 0) {
        return R.fail("注册失败，请稍后再试");
    }

    // 获取 shopId photo
    List<String> photo = registerShop.getPhoto();
    CheShop registeredShop = cheShopService.selectCheShopByPhone(perPhone);
    Long shopId = registeredShop.getShopId();
    AddListImage addListImage = new AddListImage();
    addListImage.setShopId(shopId);
    addListImage.setPhoto(photo);

    // 插入店铺图片
    cheShopImageService.insertCheShopImages(addListImage);

    // 返回注册结果

    return R.ok();
}






}
